﻿package product;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;

import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Color;

import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

import java.awt.Component;

import javax.swing.Box;
import javax.swing.ImageIcon;

public class SemiChul extends JInternalFrame {
		SemiChul sc = this;
		ProductMain3 pm;
		Connection conn;

	ChulList ChulList = null;
	ChulGData cgd;
	JButton chulbtn;
	JButton tbtn;
	JButton chullistbtn;
	private JLabel snow;
	JTextField status;

	public SemiChul(ProductMain3 p, Connection conn) {
		super("출퇴근 입력", false, true, false, true);
		addInternalFrameListener(new InternalFrameAdapter() {
			@Override
			public void internalFrameClosing(InternalFrameEvent e) {
				logout();
			}
		});
		getContentPane().setBackground(Color.WHITE);
		this.pm = p;
		this.conn = conn;
		setBounds(100, 100, 273, 260);
		getContentPane().setLayout(null);
		getContentPane().add(getChulbtn());
		getContentPane().add(getTbtn());
		getContentPane().add(getChullistbtn());
		getContentPane().add(getStatus());
		getContentPane().add(getSnow());
	}

	private JButton getChulbtn() {
		if (chulbtn == null) {
			chulbtn = new JButton("출근");
			chulbtn.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					//---------------추가중----------------------------------//
					try{
						String sql = "insert into inouthistory(his_num, memberid, inout_type) values(hisnum.nextval,?,?)";
						PreparedStatement pstmt = pm.conn.prepareStatement(sql);
						pstmt.setString(1, pm.logincode);
						pstmt.setInt(2, 1);	 // 출근일때 1		
						pstmt.executeUpdate(); // 입력 수정 삭제 할때 전부
						pstmt.close();
						pm.openChulList();
						chulbtn.setVisible(false);
						tbtn.setVisible(true);
					}catch(Exception ex){
						ex.printStackTrace();
					}
					//---------------추가중----------------------------------//
				}
			});
			chulbtn.setBounds(12, 108, 109, 23);
		}
		return chulbtn;
	}

	private JButton getTbtn() {
		if (tbtn == null) {
			tbtn = new JButton("퇴근");
			tbtn.setVisible(false);
			tbtn.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					//---------------추가중----------------------------------//
					try{
						String sql = "insert into inouthistory(his_num, memberid, inout_type) values(hisnum.nextval,?,?)";
						PreparedStatement pstmt = pm.conn.prepareStatement(sql);
						pstmt.setString(1, pm.logincode);
						pstmt.setInt(2, 0);		//퇴근일때 0	
						pstmt.executeUpdate(); // 입력 수정 삭제 할때 전부
						pstmt.close();		
						pm.openChulList();
						chulbtn.setVisible(true);
						tbtn.setVisible(false);
					}catch(Exception ex){
						ex.printStackTrace();
					}
					//---------------추가중----------------------------------//
				}
			});
			tbtn.setBounds(133, 108, 109, 23);
		}
		return tbtn;
	}
	private JButton getChullistbtn() {
		if (chullistbtn == null) {
			chullistbtn = new JButton("출퇴근목록");
			chullistbtn.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					pm.openChulList();
				}
			});
			chullistbtn.setBounds(12, 141, 230, 23);
		}
		return chullistbtn;
	}
	private JLabel getSnow() {
		if (snow == null) {
			snow = new JLabel("");
			snow.setIcon(new ImageIcon("C:\\Users\\jhta\\Downloads\\이미지\\아이콘\\눈.gif"));
			snow.setBounds(-40, 0, 350, 197);
		}
		return snow;
	}
	private JTextField getStatus() {
		if (status == null) {
			status = new JTextField();
			status.setDisabledTextColor(Color.PINK);
			status.setEditable(false);
			status.setColumns(10);
			status.setBounds(12, 10, 230, 23);
		}
		return status;
	}
	
	public void logout(){
		pm.closeChulList();
		pm.closeMemberMonth2();
	}
	
	public void loginname(){
		try {
			String sql = "select irum from memberdata where memberid = '" + pm.logincode + "'";
			PreparedStatement pstmt = pm.conn.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			ResultSetMetaData rsm = rs.getMetaData();
			while(rs.next()){   // 입력한 아이디와 비번과 맞는 데이터가 있으면
				if(!rs.getString(1).trim().equals("")){
					status.setText(rs.getString(1) + "님이 로그인 하셨습니다.");
					return;
				}
			}
			pstmt.executeUpdate(); // 입력 수정 삭제 할때 전부
			rs.close();
			pstmt.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
